local zhuixiong = fk.CreateSkill {
  name = "lingling__zhuixiong",
}
local U = require "packages/utility/utility"
Fk:loadTranslationTable{
  ["lingling__zhuixiong"] = "追凶",
  [":lingling__zhuixiong"] = "回合结束时，你从随机两名角色中猜测直到你的下个出牌阶段结束，其中哪名角色先于其出牌阶段内造成伤害，若猜对，其造成此伤害后你可以摸两张牌或对其发动“升堂”。",

  ["#lingling__zhuixiong-choose"]="追凶：猜测他们中哪一位在其出牌阶段内先造成伤害",
  ["#lingling__zhuixiong-pick"]="追凶：凶手是 %dest ！你准备做些什么？",
  ["#lingling__zhuixiong-draw"]="摸两张牌",
  ["#lingling__zhuixiong-go"]="对其发动“升堂”！",
  ["@@lingling__zhuixiong_guess"]="追凶中……",

  ["#lingling__zhuixiong_good"]="天理昭昭！你哪里逃！",
}
zhuixiong:addEffect(fk.TurnEnd,{
  can_trigger = function (self, event, target, player, data)
    if target == player and player:hasSkill(zhuixiong.name) then
      return #player.room.alive_players > 1
    end
  end,
  on_cost = function (self, event, target, player, data)
    return true
  end,
  on_trigger = function (self, event, target, player, data)
    local room = player.room
    local ids = room.alive_players
    if #ids>1 then
      local tos = table.random(ids,2)
      local to = room:askToChoosePlayers(player, {
        targets = tos,
        min_num = 1,
        max_num = 1,
        prompt = "#lingling__zhuixiong-choose",
        skill_name = zhuixiong.name,
        cancelable = false,
        no_indicate = true,
      })
      if #to>0 then
        local mark = player:getTableMark("@@lingling__zhuixiong_guess")
        table.insert(mark,{table.map(tos, Util.IdMapper),to[1].id})
        room:setPlayerMark(player,"@@lingling__zhuixiong_guess",mark)
      end
    end
  end,
})
zhuixiong:addEffect(fk.Damage,{
  can_refresh = function (self, event, target, player, data)
    if #player:getTableMark("@@lingling__zhuixiong_guess")>0 then
        if target.phase == Player.Play then
      for _, group in ipairs(player:getTableMark("@@lingling__zhuixiong_guess")) do
          if table.contains(group[1],data.from.id) then
            return true
          end
      end
    end
      return false
    end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
      local mark = player:getTableMark("@@lingling__zhuixiong_guess")
        for i=#mark,1,-1 do
          if table.contains(mark[i][1],data.from.id) then
            if mark[i][2] == data.from.id then
              room:sendLog{
                type = "#lingling__zhuixiong_good",
                toast = true,
              }
              room:doIndicate(player.id,{data.from.id})
              local choices = {"#lingling__zhuixiong-draw"}
              if data.from:isAlive() and not data.from:isKongcheng() then
                local can = true
                  for _, cid in ipairs(data.from:getCardIds('h')) do
                    if player:cardVisible(cid) then
                      can = false break end
                    end
                    if can == true then
                      table.insertIfNeed(choices,"#lingling__zhuixiong-go")
                    end
                  end
                  table.insertIfNeed(choices,"Cancel")
                  if #choices>0 then
                    local choise = room:askToChoice(player, {
          choices = choices,
          skill_name = zhuixiong.name,
          prompt = "#lingling__zhuixiong-pick::"..data.from.id,
          {"#lingling__zhuixiong-draw","#lingling__zhuixiong-go","Cancel"}
        })
                        if choise == "#lingling__zhuixiong-draw" then
                          room:drawCards(player,2,zhuixiong.name)
                        elseif choise == "#lingling__zhuixiong-go" then
                          room:askToUseActiveSkill(player, {
        skill_name = "lingling__shengtang",
        prompt = "#lingling__shengtang-prompt",
        cancelable = false,
        no_indicate = false,
      })
                        end
                      end
            end
            table.remove(mark,i) 
          end
      end
      if #mark == 0 then
        room:setPlayerMark(player,"@@lingling__zhuixiong_guess",0)
      else
        room:setPlayerMark(player,"@@lingling__zhuixiong_guess",mark)
        end
  end,
})
zhuixiong:addEffect(fk.EventPhaseEnd,{
  can_refresh = function (self, event, target, player, data)
    if #player:getTableMark("@@lingling__zhuixiong_guess")>0 then
        return player.phase == Player.Play
  end
  end,
  on_refresh = function (self, event, target, player, data)
    local room = player.room
      room:setPlayerMark(player,"@@lingling__zhuixiong_guess",0)
  end,
})
return zhuixiong